package org.neurone.userspace.userinfo.impl;

import java.util.Date;

import org.neurone.domain.Message;
import org.neurone.domain.alter.MessageAlter;
import org.neurone.userspace.dao.DaoServer;
import org.neurone.userspace.dao.MessageDao;
import org.neurone.userspace.userinfo.MessageFace;

public class MessageFaceLocal extends MessageAlter implements MessageFace {
	private MessageDao messageDao;
	protected boolean persistentAvailable;
	
	private final String STATUS_HAS_READ = "已读";
	
	
	/**
	 * 被保护的构造函数
	 * 除了factory类型，不允许其它调用方式。
	 * 初始化一个本类型对象必须使用factory方法或者
	 * 1、new MessageFaceLocal()
	 * 2、setMessageDao()
	 * 3、initByMessageID()
	 */
	protected MessageFaceLocal()
	{
	}
	
	
	
	public MessageDao getMessageDao() {
		return messageDao;
	}



	protected void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	protected void initByMessageID(Long messageID)
	{
		initMessageInfo(this.messageDao.find(messageID));
	}

	private void initMessageInfo(Message message) {
		this.persistentAvailable = message != null;
		if(this.persistentAvailable)
		{
			this.messageID = message.getMessageID();
			
			this.setAvailable(message.isAvailable());
			this.setFrom(message.getFrom());
			this.setMessageType(message.getMessageType());
			this.setPostDate(message.getPostDate());
			this.setPriority(message.getPriority());
			this.setStatus(message.getStatus());
			this.setText(message.getText());
			this.setTo(message.getTo());
			this.setTopic(message.getTopic());
			
			this.setBelongToMessage(message.getBelongToMessage());
		}
	}

	@Override
	public void delete() {
		this.messageDao.deleteMessage(this.messageID);
	}

	@Override
	public void gotIt() {
		this.setStatus(STATUS_HAS_READ);
		flush();
	}

	@Override
	public Message getBelongToMessage() {
		// TODO Auto-generated method stub
		return this.belongToMessage;
	}

	@Override
	public String getFrom() {
		// TODO Auto-generated method stub
		return this.from;
	}

	@Override
	public Long getMessageID() {
		// TODO Auto-generated method stub
		return this.messageID;
	}

	@Override
	public String getMessageType() {
		// TODO Auto-generated method stub
		return this.messageType;
	}

	@Override
	public Date getPostDate() {
		// TODO Auto-generated method stub
		return this.postDate;
	}

	@Override
	public int getPriority() {
		// TODO Auto-generated method stub
		return this.priority;
	}

	@Override
	public String getStatus() {
		// TODO Auto-generated method stub
		return this.status;
	}

	@Override
	public String getText() {
		// TODO Auto-generated method stub
		return this.text;
	}

	@Override
	public String getTo() {
		// TODO Auto-generated method stub
		return this.to;
	}

	@Override
	public String getTopic() {
		// TODO Auto-generated method stub
		return this.topic;
	}

	@Override
	public boolean isAvailable() {
		// TODO Auto-generated method stub
		return this.available;
	}

	public void setMessageType(String messageType) {
		this.messageType = messageType;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setPostDate(Date postDate) {
		this.postDate = postDate;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void setAvailable(boolean available) {
		this.available = available;
	}

	public void setBelongToMessage(Message belongToMessage) {
		this.belongToMessage = belongToMessage;
	}


	@Override
	public void flush() {
		this.messageDao.updateMessage(this);
	}

	@Override
	public boolean isPersistentAvailable() {
		return this.persistentAvailable;
	}
}
